SUBROUTINE probemax(imun,iage,Agrid,ageN)
  USE commonvars
  IMPLICIT NONE
  INCLUDE 'mpif.h'

  INTEGER, INTENT(IN) :: imun,iage, ageN, Agrid(ageN)
  INTEGER             :: agei, ipast, ifu, ipr, ifine, iterm, iedu, iwealth, ipau, isteal, iq, elap, agec, ieduc, iwealthc
  REAL(8)             :: probage
  
!  REAL(8)             :: temp, temp1 !, temp2 !, temp3

  !We compute the probabilities to use in emaxtemp here so that we can do it only once
  ALLOCATE(probemax1(Neduc,Nnterms,Nfines,Npr,Nfunds,Npast,Npaudits,Nwealth,Nsteal,Nq,Nelap,ageN,Neduc,Nwealth),&
           probemax2(Neduc,Nnterms,Npast,Npaudits,Nwealth,Nsteal,Nq,Nelap,ageN,Neduc,Nwealth),&
           probemax3(ageN,Neduc,Nnterms,Npast,Npaudits,Nwealth,Nsteal,Nq,Nelap,ageN,Neduc,Nwealth),&
           probemax4(ageN,Neduc,Nnterms,Npast,Npaudits,Nwealth,Nsteal,Nq,Nelap,ageN,Neduc,Nwealth))
  
  probemax1 = 0d0
  probemax2 = 0d0
  probemax3 = 0d0
  probemax4 = 0d0
  probage=(1d0/DBLE(ageN))

  IF (iage < Retage) THEN
     
     DO iwealth = 1, Nwealth
        DO ipau = 1, Npaudits
           DO ipast = 1, Npast
              DO ifu = 1, Nfunds
                 DO ipr = 1, Npr
                    DO ifine = 1, Nfines
                       DO iterm = 1, Nnterms
                          DO iedu = 1, Neduc
                             DO iq = 1, Nq
                                DO isteal = 1, Nsteal
                                   DO elap = 1, Nelap
                                      ! Challenger's observable characteristics
                                      DO iwealthc = 1, Nwealth
                                         DO ieduc = 1, Neduc
                                            DO agec = 1, agen
                                      
                                               ! We don't need to keep track of age because age is fixed when we compute the expected value function of an incumbent
                                               probemax1(iedu,iterm,ifine,ipr,ifu,ipast,ipau,iwealth,isteal,iq,elap,agec,ieduc,iwealthc) = Pr_i_win(imun,iage,ipast,iedu,iwealth,ipau,iterm,isteal,iq,elap,Agrid(agec),ieduc,iwealthc)*nonpardist_fu_pr(ifu,ipr)*probage*probedu(ieduc)*pwealth(iwealthc)
                                               
                                            END DO
                                         END DO
                                      END DO
                                   END DO
                                END DO
                             END DO
                          END DO
                       END DO
                    END DO
                 END DO
              END DO
           END DO
        END DO
     END DO
     
     DO iwealth = 1, Nwealth
        DO ipau = 1, Npaudits
           DO ipast = 1, Npast
              DO iterm = 1, Nnterms
                 DO iedu = 1, Neduc
                    DO iq = 1, Nq
                       DO isteal = 1, Nsteal
                          DO elap = 1, Nelap
                             ! Challenger's observable characteristics
                             DO iwealthc = 1, Nwealth
                                DO ieduc = 1, Neduc
                                   DO agec = 1, agen

                                      ! We add up probabilities over ifu, ipr, and ifine, because if a mayor loses the election, the value function doesn't depend on them
                                      probemax2(iedu,iterm,ipast,ipau,iwealth,isteal,iq,elap,agec,ieduc,iwealthc) = (1d0-Pr_i_win(imun,iage,ipast,iedu,iwealth,ipau,iterm,isteal,iq,elap,Agrid(agec),ieduc,iwealthc))*probage*probedu(ieduc)*pwealth(iwealthc)

                                   END DO
                                END DO
                             END DO
                          END DO
                       END DO
                    END DO
                 END DO
              END DO
           END DO
        END DO
     END DO

!!$     temp = 0d0
!!$     DO ifine = 1, Nfines
!!$        DO ifu = 1, Nfunds
!!$           DO ipr = 1, Npr
!!$              DO iwealthc = 1, Nwealth
!!$                 DO ieduc = 1, Neduc
!!$                    DO agec = 1, agen
!!$                       
!!$                       temp = temp + probemax1(1,1,ifine,ipr,ifu,1,1,1,1,1,1,agec,ieduc,iwealthc)
!!$                       write(*,'(a,4f20.10)') 'temp', temp, probemax1(1,1,ifine,ipr,ifu,1,1,1,1,1,1,agec,ieduc,iwealthc), Pr_i_win(imun,iage,1,1,1,1,1,1,1,1,1,1,1)
!!$
!!$                    END DO
!!$                 END DO
!!$              END DO
!!$           END DO
!!$        END DO
!!$     END DO
!!$
!!$     write(*,'(a,3f20.10)') 'probemax', temp
!!$     
!!$     temp1 = 0d0
!!$     DO iwealthc = 1, Nwealth
!!$        DO ieduc = 1, Neduc
!!$           DO agec = 1, agen
!!$              
!!$              temp1 = temp1 + probemax2(1,1,1,1,1,1,1,1,agec,ieduc,iwealthc)
!!$              write(*,'(a,4f20.10)') 'temp1', temp1, probemax2(1,1,1,1,1,1,1,1,agec,ieduc,iwealthc), 1-Pr_i_win(imun,iage,1,1,1,1,1,1,1,1,1,1,1)
!!$
!!$           END DO
!!$        END DO
!!$     END DO
!!$
!!$     write(*,'(a,3f20.10)') 'probemax', temp, temp1
     
     DO iwealth = 1, Nwealth
        DO ipau = 1, Npaudits
           DO ipast = 1, Npast
              DO iterm = 1, Nnterms
                 DO iedu = 1, Neduc
                    DO agei = 1, ageN
                       DO iq = 1, Nq
                          DO isteal = 1, Nsteal
                             DO elap = 1, Nelap
                                ! Challenger's observable characteristics
                                DO iwealthc = 1, Nwealth
                                   DO ieduc = 1, Neduc
                                      DO agec = 1, agen
                                         
                                         ! Here we need to keep track of age because it is the age of the incumbent currently in power and not the individual we are considering
                                         probemax3(agei,iedu,iterm,ipast,ipau,iwealth,isteal,iq,elap,agec,ieduc,iwealthc) = Pr_i_win(imun,Agrid(agei),ipast,iedu,iwealth,ipau,iterm,isteal,iq,elap,Agrid(agec),ieduc,iwealthc)*probage*probedu(ieduc)*pwealth(iwealthc)

                                      END DO
                                   END DO
                                END DO
                             END DO
                          END DO
                       END DO
                    END DO
                 END DO
              END DO
           END DO
        END DO
     END DO
     
     DO iwealth = 1, Nwealth
        DO ipau = 1, Npaudits
           DO ipast = 1, Npast
              DO iterm = 1, Nnterms
                 DO iedu = 1, Neduc
                    DO agei = 1, ageN
                       DO iq = 1, Nq
                          DO isteal = 1, Nsteal
                             DO elap = 1, Nelap
                                ! Challenger's observable characteristics
                                DO iwealthc = 1, Nwealth
                                   DO ieduc = 1, Neduc
                                      DO agec = 1, agen

                                         ! We compute the probability that an incumbent loses the election
                                         probemax4(agei,iedu,iterm,ipast,ipau,iwealth,isteal,iq,elap,agec,ieduc,iwealthc) = (1d0 - Pr_i_win(imun,Agrid(agei),ipast,iedu,iwealth,ipau,iterm,isteal,iq,elap,Agrid(agec),ieduc,iwealthc))*probage*probedu(ieduc)*pwealth(iwealthc)

                                      END DO
                                   END DO
                                END DO
                             END DO
                          END DO
                       END DO
                    END DO
                 END DO
              END DO
           END DO
        END DO
     END DO

  ELSE

     !The incumbent cannot be reelecter for agei >= Retage
     probemax1 = 0d0

     DO iwealthc = 1, Nwealth
        DO ieduc = 1, Neduc
           DO agec = 1, agen
              
              probemax2(:,:,:,:,:,:,:,:,agec,ieduc,iwealthc) = probage*probedu(ieduc)*pwealth(iwealthc)
              
           END DO
        END DO
     END DO

     probemax3 = 0d0

     DO iwealthc = 1, Nwealth
        DO ieduc = 1, Neduc
           DO agec = 1, agen
              
              probemax4(:,:,:,:,:,:,:,:,:,agec,ieduc,iwealthc) = probage*probedu(ieduc)*pwealth(iwealthc)
              
           END DO
        END DO
     END DO
          
  END IF
     
END SUBROUTINE probemax
